import create from "zustand";
import { nanoid } from "nanoid";

/*const getLocalStorage = (key) => JSON.parse(window.localStorage.getItem(key));
const setLocalStorage = (key, value) =>
  window.localStorage.setItem(key, JSON.stringify(value));*/

export const useStore = create((set) => ({
  texture: "dirt",
  cubes: [
    {
      key: nanoid(),
      pos: [1, 5, 1],
      texture: "dirt",
    },
    {
      key: nanoid(),
      pos: [3, 3, 1],
      texture: "grass",
    },
    {
      key: nanoid(),
      pos: [5, 1, 1],
      texture: "glass",
    },
  ],
  addCube: (x, y, z) => {
    console.log(`add cube at ${x} ${y} ${z}`);
    set((prev) => ({
      cubes: [
        ...prev.cubes,
        {
          key: nanoid(),
          pos: [x, y, z],
          texture: prev.texture,
        },
      ],
    }));
  },
  removeCube: (x, y, z) => {
    set((prev) => ({
      cubes: prev.cubes.filter((cube) => {
        const [X, Y, Z] = cube.pos;
        return X !== x || Y !== y || Z !== z;
      }),
    }));
  },
  setTexture: (texture) => {
    set(() => ({
      texture,
    }));
  },
  setAllCubes: (cubes) => {
    set(() => ({
      cubes,
    }));
  },
  isThirdPerson: false,
  setThirdPerson: (isThird) => set({ isThirdPerson: isThird }),
}));
